/* 
 * $Id$
 *
 *
 * Ryan Boyd 
 *
 *!\file intdefs.c
 * \brief Implementation for 8, 16, 32 and 64 bit integer operations.
 *
**/

#include "intdefs.h"

/*!fn inline int is_bit_on(MemByte mb, Byte mask)
 * \brief Test whether bit(s) in \a mask are on.
 * \param mb pointer to the \a Byte to test.
 * \param mask A \a Byte with the bits to test.
 * \return an \a int (1 if true, 0 otherwise).
 * \warning If \a mask contains multiple bits to test and if
 * any one of those bits is off in \a mb - \a is_bit_on() will
 * return 0.  
 */
inline int is_bit_on(MemByte mb, Byte mask) {
  return( (*mb & mask) ? 1 : 0);
}

/*!\fn inline Byte bit_all_off(MemByte mb)
 * \brief Turn all bits in \a Byte pointed to by \a mb off.
 * \param mb pointer to the \a Byte to alter.
 * \return \a Byte with all bits off. 
 */
inline Byte bit_all_off( MemByte mb ) {
  return( *mb &= ~*mb ); 
}


/*!\fn inline Byte bit_all_on(MemByte mb)
 * \brief Turn all bits in \a Byte pointed to by \a mb on.
 * \param mb The \a Byte to alter.
 * \return a \a Byte with all bits off. 
 */
inline Byte bit_all_on(MemByte mb ) {
  return( *mb |= ~*mb );
}

/*!\fn inline Byte bit_all_flip(MemByte mb)
 * \brief Flip all bits in \a mb to their opposite.
 * \param mb The \a Byte to alter.
 * \return a \a Byte with all bits flipped. 
 */
inline Byte bit_all_flip(MemByte mb ) {
  return( *mb = ~*mb );
}

/*!\fn inline Byte bit_on(MemByte mb, Byte mask )
 * \brief Turn \a mask bit(s) in \a Byte pointed to by \a mb ON.
 * \param mb pointer to the \a Byte to alter.
 * \param mask A \a Byte with the bit(s) you want to change.
 * \return \a Byte with \a mask bit(s) turned ON.
 */ 
inline Byte bit_on(MemByte mb, Byte mask) {
  return( *mb |= mask );
}


/*!\fn inline Byte bit_off(MemByte mb, Byte mask )
 * \brief Turn \a mask bit(s) in \a Byte pointed to by \a mb OFF.
 * \param mb pointer to the \a Byte to alter.
 * \param mask A \a Byte with the bit(s) you want to change.
 * \return \a Byte with \a mask bit(s) turned OFF.
 */ 
inline Byte bit_off(MemByte mb, Byte mask) {
  return( (Byte)(*mb &= ~mask) );
} 

/*!\fn inline Byte flip_bit(MemByte mb, Byte mask )
 * \brief Flip bits to their opposite.
 * \param mb pointer to the \a Byte to alter.
 * \param mask A \a Byte with the bit(s) you want to change.
 * \return \a Byte with \a mask bit(s) flipped opposite their current value.
 * \warning See the comments regarding the is_bit_on() function.
 */ 
inline Byte bit_flip(MemByte mb, Byte mask) {
  if( is_bit_on(mb, mask) )
    return( *mb &= ~mask );
  else
    return( *mb |= mask );
}


